package com.example.nbshangpping.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.nbshangpping.R;
import com.example.nbshangpping.login.LoginFragment;
import com.example.nbshangpping.login.LoginVPAdapter;
import com.example.nbshangpping.login.RegisterFragment;

import java.util.ArrayList;
import java.util.List;

public class LoginRegActivity extends AppCompatActivity {
    TextView logintv,regtv;
    ViewPager loginvp;
    List<Fragment>fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_reg);
        //接受上一个传来的数据   0-登录  1-注册
        int flag=getIntent().getIntExtra("flag",0);
        initView();
        setlisener();
        initPager();     //声明viewpager的展示页
        setPagerPos(flag);
    }
//设置显示位置
    private void setPagerPos(int pos) {
        if (pos == 0) {
             logintv.setTextColor(Color.rgb(241,104,173));
             regtv.setTextColor(Color.WHITE);
        }else if (pos == 1){
            regtv.setTextColor(Color.rgb(241,104,173));
            logintv.setTextColor(Color.WHITE);
        }
        loginvp.setCurrentItem(pos);
    }

    /*声明viewpager的展示页*/
    private void initPager() {
        fragmentList=new ArrayList<>();
        fragmentList.add(new LoginFragment());
        fragmentList.add(new RegisterFragment());
        //将fragment放到viewpager里面，展示在activity上
        LoginVPAdapter vpAdapter=new LoginVPAdapter(getSupportFragmentManager(),fragmentList);
        loginvp.setAdapter(vpAdapter);    //配置适配器
    }

    /*设置监听事件*/
    private void setlisener() {
        logintv.setOnClickListener(onClickListener);
        regtv.setOnClickListener(onClickListener);
        loginvp.addOnPageChangeListener(listener);
    }
ViewPager.OnPageChangeListener listener=new ViewPager.SimpleOnPageChangeListener(){
    @Override
    public void onPageSelected(int position) {
        setPagerPos(position);
    }
};
    View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id=v.getId();
            switch (id) {
                case R.id.login_tv_log:
                    setPagerPos(0);
                    break;
                case R.id.login_tv_reg:
                    setPagerPos(1);
                    break;
            }

        }
    };
    /*初始化控件*/
    private void initView() {
        logintv=findViewById(R.id.login_tv_log);
        regtv=findViewById(R.id.login_tv_reg);
        loginvp=findViewById(R.id.login_vp);
    }
}